import paramiko

from servercon.exceptions import *


class Linux(object):
    def __init__(self,hostname,port,username,password):
        self.client = paramiko.SSHClient()
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password

    def connect(self):
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
            self.client.connect(
                self.hostname,self.port,self.username,self.password
            )
        except Exception as e:
            raise ConncetError(e)

    def close(self):
        try:
            self.client.close()
        except Exception as e:
            raise CloseSSHError(e)

    def execute(self,cmd):
        stdin,stdout,stderr = self.client.exec_command(cmd)
        return {
            "stdin":stdin,"stdout":stdout,"stderr":stderr
        }



